/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.harding.numguess;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alberto
 */
@WebServlet(name = "NumGuessServlet", urlPatterns = {"/NumGuessServlet"})
public class NumGuessServlet extends HttpServlet 
{
    private String _message = "Guess a number between 1 and 10: ";
    private int _randomNumber = 0;
    private String _number = "";
    private Random _random;
    HttpSession _session;
    
    @Override
    public void init() throws ServletException
    {
       super.init();
       _random = new Random();
    }
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        _session = request.getSession();
        if (_session.getAttribute("number") == null)
        {
            _randomNumber = _random.nextInt(9) + 1;
            _session.setAttribute("number", _randomNumber);
        }
        else
        {
            _randomNumber = Integer.parseInt(_session.getAttribute("number").toString());
        }
        
        
        try 
        {
            /*
             * TODO output your page here. You may use following sample code.
             */
            _number = request.getParameter("number");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>De Pena - HMK #6</title>");            
            out.println("</head>");
            out.println("<body>");
            
            try
            {
                Integer guess = Integer.parseInt(_number);
                if (guess < 11 && guess > 0 )
                {
                    if (guess < _randomNumber)
                    {
                        out.println("<b>Too low!</b><br/><br/>");
                    }
                    else if (guess > _randomNumber)
                    {
                        out.println("<b>Too high!</b><br/><br/>");
                    }
                    else if (guess == _randomNumber)
                    {
                        out.println("<b>Correct!</b><br/><br/>");
                         _randomNumber = _random.nextInt(9) + 1;
                         _session.setAttribute("number", _randomNumber);
                    }
                }
            }
            catch (NumberFormatException nfe)
            {
                out.println("<b>Please enter a number!</b><br/><br/>");
            }
            
            out.println("<form method='post'>");
            out.println(_message);
            out.println("<input type='text' name='number' size='2'/><br />");
            out.println("<input type='submit' value='Submit' style='width: 80px; height: 30px'/>");
            out.println("</from");
            out.println("</body>");
            out.println("</html>");
        } 
        finally 
        {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {   
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
       processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() 
    {
        return "Short description";
    }// </editor-fold>
}